package cn.yinyu.queue.module.ecg.service.room;

import java.util.*;
import javax.annotation.Resource;

import cn.yinyu.queue.framework.common.pojo.PageResult;
import cn.yinyu.queue.module.ecg.controller.admin.room.vo.MonitorInfoVO;
import cn.yinyu.queue.module.ecg.dal.dataobject.queue.QueueDO;
import cn.yinyu.queue.module.ecg.dal.dataobject.room.RoomDO;
import cn.yinyu.queue.module.ecg.service.queue.QueueService;
import cn.yinyu.queue.module.system.api.oauth2.OAuth2TokenApi;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import cn.yinyu.queue.module.ecg.controller.admin.room.vo.RoomPageReqVO;
import cn.yinyu.queue.module.ecg.controller.admin.room.vo.RoomSaveReqVO;
import cn.yinyu.queue.module.ecg.dal.dataobject.room.RoomStatisticsDO;
import cn.yinyu.queue.module.ecg.dal.mysql.room.RoomMapper;
import cn.yinyu.queue.module.ecg.enums.BedStatusEnum;
import cn.yinyu.queue.framework.common.util.object.BeanUtils;

import static cn.yinyu.queue.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.yinyu.queue.module.ecg.enums.ErrorCodeConstants.ROOM_NOT_EXISTS;

/**
 * 诊室和诊疗床 Service 实现类
 *
 * @author 垠域源码
 */
@Service
@Validated
public class RoomServiceImpl implements RoomService {

    @Resource
    private RoomMapper roomMapper;

    @Resource
    private OAuth2TokenApi oAuth2TokenApi;

    @Resource
    QueueService queueService;

    @Override
    public Integer createRoom(RoomSaveReqVO createReqVO) {
        // 插入
        RoomDO room = BeanUtils.toBean(createReqVO, RoomDO.class);
        roomMapper.insert(room);
        // 返回
        return room.getId();
    }

    @Override
    public void updateRoom(RoomSaveReqVO updateReqVO) {
        // 校验存在
        validateRoomExists(updateReqVO.getId());
        // 更新
        RoomDO updateObj = BeanUtils.toBean(updateReqVO, RoomDO.class);
        roomMapper.updateById(updateObj);
    }

    @Override
    public void deleteRoom(Integer id) {
        // 校验存在
        validateRoomExists(id);
        // 删除
        roomMapper.deleteById(id);
    }

    private void validateRoomExists(Integer id) {
        if (roomMapper.selectById(id) == null) {
            throw exception(ROOM_NOT_EXISTS);
        }
    }

    @Override
    public RoomDO getRoom(Integer id) {
        return roomMapper.selectById(id);
    }

    @Override
    public RoomDO getRoomByIP(String ip) {
        List<RoomDO> roomDOList = roomMapper.queueByIp(ip);
        Optional<RoomDO> optionalQueueDO = roomDOList.stream().filter(item -> StringUtils.hasLength(item.getIp())).findFirst();
        if (!optionalQueueDO.isPresent()) {
            return null;
        }

        return optionalQueueDO.get();
    }

    @Override
    public PageResult<RoomDO> getRoomPage(RoomPageReqVO pageReqVO) {
        return roomMapper.selectPage(pageReqVO);
    }

    @Override
    public List<RoomStatisticsDO> roomStatistics() {
        return roomMapper.roomStatistic();
    }

    @Override
    public List<RoomDO> simpleRoomList() {
        List<BedStatusEnum> bedStatusEnumList = new ArrayList<BedStatusEnum>();
        bedStatusEnumList.add(BedStatusEnum.OPENING);
        bedStatusEnumList.add(BedStatusEnum.DOCTOR_ON);
        bedStatusEnumList.add(BedStatusEnum.PAUSE);
        return  roomMapper.simpleRoomList(bedStatusEnumList);
    }

    @Override
    public void resetRoom(Boolean needCloseBed) {
        queueService.startResetRoom(needCloseBed);
    }

    @Override
    public MonitorInfoVO getMonitorInfo() {
        return queueService.getMonitorInfo();
    }
}